package android.app.gfestival.adapter;

import java.util.List;

import android.app.gfestival.R;
import android.app.gfestival.rss.RSSFeed;
import android.app.gfestival.rss.RSSItem;
import android.app.gfestival.utils.DateTimeUtil;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NewsAdapter extends BaseAdapter{

	private Context context;
	private ImageLoader loader;
	private List<RSSItem> items;

	public NewsAdapter(Context context, List<RSSItem> items){
		this.context = context;
		this.items = items;
		loader = new ImageLoader(context);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Holder holder = null;
		if(convertView == null){
			holder = new Holder();
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.news_item, null);
			holder.icon = (ImageView) convertView.findViewById(R.id.imageview_icon);
			holder.desc = (TextView) convertView.findViewById(R.id.textview_desc);
			holder.title = (TextView) convertView.findViewById(R.id.textview_title);
			holder.date = (TextView) convertView.findViewById(R.id.textview_date);
			convertView.setTag(holder);
		}else {
			holder = (Holder) convertView.getTag();
		}
//		loader.DisplayImage(items.get(position).getImage(), context, holder.icon);
//		Log.i("News", "---->" + items.get(position)._);
		holder.desc.setText(items.get(position).getDescription());
		holder.title.setText(items.get(position).getTitle());
//		String day = DateTimeUtil.converNewsDate(items.get(position).getPubDate().substring(4, items.get(position).getPubDate().length() - 6),"EEEE dd.MM.yy HH:mm:ss");
		String day = DateTimeUtil.convertDate(items.get(position).getPubDate(),"EEEE dd.MM.yy HH:mm:ss");
		day = DateTimeUtil.convertDay(day, context);
		holder.date.setText("(" + day + ")");
		return convertView;
	}
	
	class Holder{
		ImageView icon;
		TextView title;
		TextView desc;
		TextView date;
	}

}
